package com.neteast.androidclient.db;

import java.util.HashMap;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.util.LoginUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract;

public class ContactDao {
	private MyDBHelp dbHelp;
	private SQLiteDatabase db;
	private Context context;
	
	public ContactDao(Context context) {
		this.context=context;
	}
	
	public void open() {
		dbHelp=new MyDBHelp(context);
		db=dbHelp.getWritableDatabase();
	}
	
	public void close() {
		dbHelp.close();
	}
	

	public void importSysContacts(HashMap<String, String> groupIdMapping,Contact contact) {
		
		
	}
	
	public void insertNewContact(Contact contact) {
		String localId = insertContact(contact);
		contact.localId=localId;
		
	}
	
	private String insertContact(Contact contact) {
		ContentValues values=new ContentValues();
		values.put("accountId", LoginUtil.getLastLoginUserId(context));
		values.put("is_read_only", contact.isReadOnly?1:0);
		values.put("version", 1);
		values.put("display_name", contact.displayName);
		values.put("nickname", contact.nickName);
		values.put("sort_key", Character.toString(contact.sortKey));
		values.put("organization", contact.organization);
		values.put("job", contact.job);
		values.put("phone", contact.phones.get(0).content);
		values.put("email", contact.emails.get(0).content);
		values.put("photo", contact.photo);
		values.put("has_photo", contact.hasPhoto?1:0);
		return Long.toString(db.insert("contacts", null, values));
	}
}
